package com.neu.cloudfactory.controler;

import com.neu.cloudfactory.bean.Equipement.Equipment;
import com.neu.cloudfactory.bean.Factory.Factory;
import com.neu.cloudfactory.bean.User.User;
import com.neu.cloudfactory.repository.EquipmentRepository;
import com.neu.cloudfactory.repository.FactoryRepository;
import com.neu.cloudfactory.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.NoSuchElementException;


@RestController
public class UserControler {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    FactoryRepository factoryRepository;
    @Autowired
    EquipmentRepository equipmentRepository;


    @RequestMapping("/user/findAll/{page}/{size}")
    public Page<User> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return userRepository.findAll(pageable);
    }

    @RequestMapping("/user/findById/{id}")
    public User findById(@PathVariable("id") Integer id){
        return userRepository.findById(id).get();
    }

    @RequestMapping("/user/update")
    public String update(@RequestBody User user){
        if(user.getId()!=null){
            user = userRepository.save(user);
            return "success";
        }else{
            return "failed";
        }
    }

    @RequestMapping("/user/search/{page}/{size}/{input}")
    public Page<User> search(@PathVariable("input") String input,@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        int id;
        try {
            id = Integer.parseInt(input);
        }catch (Exception e){
            id = -1;
        }
        return userRepository.findAllByIdOrNameContainingOrAccountContainingOrConnectionContaining(id,input,input,input,pageable);
    }

    @RequestMapping("/user/deleteById/{id}")
    public String delete(@PathVariable("id") Integer id){
        try{
            User user = userRepository.findById(id).get();
            if(user.getTypeid()==1){
                deleteByManagerid(user.getId());
            }
            userRepository.delete(user);
            return "success";
        }catch (NoSuchElementException e){
            return "failed";
        }
    }

    @RequestMapping("/checkAccout/{account}")
    public int checkAccout(@PathVariable("account") String account){
        if(userRepository.existsByAccount(account)){
            return 1;
        }else {
            return 0;
        }
    }

    public boolean deleteByManagerid(int managerid){
        Factory factory = factoryRepository.findByManagerid(managerid);
        if(factory!=null){
            equipmentRepository.deleteAllByFactoryid(factory.getId());
            factoryRepository.delete(factory);
        }
        return true;
    };
}
